package com.pz.util;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;

import java.io.*;

public class FileUtil {
    private File file;
    private String path;
    private File[] files;

    /**
     * 判断指定文件是否存在
     * */
    public boolean isFileExist() {
        return this.file.isFile();
    }

    /**
     * 读取一个指定文件内容
     * */
    public String readFile()throws IOException {
        if(isFileExist() == false){
            return "此文件为空！";
        }
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            if (path.endsWith(".doc")){
                try (InputStream is = new FileInputStream(this.file)){
                    WordExtractor re = new WordExtractor(is);
                    sb.append(re.getText());
                    re.close();
                }
            }else if(path.endsWith(".docx")){
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                sb.append(extractor.getText());
                extractor.close();
            }else if(path.endsWith(".txt")){
                try(FileInputStream fis = new FileInputStream(this.file)){
                    br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
                    String readLine = null;
                    while((readLine = br.readLine()) != null){
                        sb.append(readLine+"\r\n");
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }finally {
                    try {
                        br.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    /**
     * 读取一个文件夹下所有的文档
     * */
    public String readFileS(String path) throws IOException{
        this.files = new File(path).listFiles();
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        for(File file : this.files){
            if(file.isFile()){
                try {
                    if (file.getName().endsWith(".txt")){
                        try(FileInputStream fis = new FileInputStream(file)){
                            br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
                            String readLine = null;
                            while((readLine = br.readLine()) != null){
                                sb.append(readLine+"\r\n");
                            }
                        }catch (IOException e){
                            e.printStackTrace();
                        }finally {
                            try {
                                br.close();
                            }catch (IOException e){
                                e.printStackTrace();
                            }
                        }
                    }else if(file.getName().endsWith(".doc")){
                        try (InputStream is = new FileInputStream(file)){
                            WordExtractor re = new WordExtractor(is);
                            sb.append(re.getText());
                            re.close();
                        }
                    }
                    else if(file.getName().endsWith(".docx")){
                        OPCPackage opcPackage = POIXMLDocument.openPackage(file.getPath());
                        POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                        sb.append(extractor.getText());
                        extractor.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();
    }

    /**
     *  读取本项目的文件
     * */
    public String readLocal(String filename) throws IOException {
        String path = this.getClass().getClassLoader().getResource("./except/"+filename).getFile();
        File file = new File(path);
        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;

        try(InputStream is = new FileInputStream(file)) {
            br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
            String readLine = null;
            while((readLine = br.readLine()) != null){
                sb.append(readLine+"\r\n");
            }
        }finally {
            br.close();
        }

        return sb.toString();
    }


    public FileUtil(String filePath) {
        this.file = new File(filePath);
        this.path = filePath;
    }
    public FileUtil(){}

}
